package com.cangshi.permission.session.entity;

import com.cangshi.permission.exception.PermissionInitException;
import com.cangshi.permission.session.exception.SessionIdCannotFindException;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;

/**
 * Created by Eoly on 2017/4/17.
 */
public class SessionCacheManager {

    private String xmlPath;
    private String sessionCacheName;
    private Cache sessionCache;
    private String basePath;
    private Boolean isSingleSignOn = false;


    public SessionCacheManager() {
        // 获取当前路径
        this.basePath = this.getClass().getClassLoader().getResource("").getPath();
    }

    // 增加一个会话
    public String addSession() {

        if (sessionCache == null) {
            throw new PermissionInitException("未在xml中初始化会话缓存（sessionCache）");
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
        String date = sdf.format(new java.util.Date());
        //设置图片名为当前时间加随机数
        String sessionId = date + new Random().nextInt();

        Session session = new Session<String, Object>(sessionId,this);

        sessionCache.put(new Element(sessionId, session));

        return sessionId;
    }

    public void addSessionAttribute(String sessionId, String key, Object value) {
        if (sessionCache == null) {
            throw new PermissionInitException("未在xml中初始化会话缓存（sessionCache）");
        }
        if (sessionCache.get(sessionId) == null) {
            throw new SessionIdCannotFindException("未找到证书会话id对应的会话");
        }
        Session<String, Object> session = getSession(sessionId);
        session.put(key, value);
        sessionCache.put(new Element(sessionId, session));
    }

    public void updateSession(String sessionId,Session<String, Object> value){
        if (sessionCache == null) {
            throw new PermissionInitException("未在xml中初始化会话缓存（sessionCache）");
        }
        if (sessionCache.get(sessionId) == null) {
            throw new SessionIdCannotFindException("未找到证书会话id对应的会话");
        }
        sessionCache.put(new Element(sessionId, value));
    }

    public void removeSession(String sessionId){
        if (sessionCache == null) {
            throw new PermissionInitException("未在xml中初始化会话缓存（sessionCache）");
        }
        if (sessionCache.get(sessionId) == null) {
            throw new SessionIdCannotFindException("未找到证书会话id对应的会话");
        }
        sessionCache.remove(sessionId);
    }

    // 获取session
    public Session<String, Object> getSession(String sessionId) {
        if (sessionCache == null) {
            throw new PermissionInitException("未在xml中初始化会话缓存（sessionCache）");
        }
        if (sessionCache.get(sessionId) == null) {
            throw new SessionIdCannotFindException("未找到证书会话id对应的会话");
        }
        return (Session<String, Object>) sessionCache.get(sessionId).getObjectValue();
    }

    public List<String> getSessionIdList(){
        return sessionCache.getKeys();
    }

    public String getXmlPath() {
        return xmlPath;
    }

    public void setXmlPath(String xmlPath) {
        this.xmlPath = xmlPath;
    }

    public String getSessionCacheName() {
        return sessionCacheName;
    }

    public void setSessionCacheName(String sessionCacheName) {
        this.sessionCacheName = sessionCacheName;
        CacheManager manager = CacheManager.create(basePath + xmlPath);
        this.sessionCache = manager.getCache(sessionCacheName);
    }

    public Boolean getSingleSignOn() {
        return isSingleSignOn;
    }

    public void setSingleSignOn(Boolean singleSignOn) {
        isSingleSignOn = singleSignOn;
    }
}
